<template>
  <el-container>
    <div class="header">
      <div class="title">苏小课直播教学平台</div>
      <div class="user-info">
        <div class="notification">
          <i class="bell-icon">🔔</i>
          <span class="badge">{{ messages.length }}</span>
        </div>
        <div class="user">您好，{{ userStore.uname }}！</div>
        <div class="actions">
          <a href="./login.html">退出</a>
        </div>
      </div>
    </div>
    <el-header>
      <div class="breadcrumb-nav">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item @click='navigateTo("teacherHome/home.html")'>首页</el-breadcrumb-item>
          <el-breadcrumb-item>消息中心</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
    </el-header>

    <el-main>
      <div class="content-wrapper">
        <div class="message-nav">
          <el-button @click="selectTab('notification')" :type="selectedTab === 'notification' ? 'primary' : 'default'">
            通知
          </el-button>
          <el-button @click="selectTab('private')" :type="selectedTab === 'private' ? 'primary' : 'default'">
            私信
          </el-button>
        </div>
        
        <div v-if="selectedTab === 'notification'">
          <div v-for="message in messages" :key="message.id" class="message-card">
            <h3>{{ message.title }}</h3>
            <p>{{ message.content }}</p>
            <p class="message-meta">
              来自: {{ message.sender }} | 时间: {{ message.time }}
            </p>
            <a href="#" @click.prevent="viewFullMessage(message)">查看全部</a>
          </div>
        </div>

        <div v-if="selectedTab === 'private'">
          <!-- 私信内容 -->
        </div>
      </div>
    </el-main>
  </el-container>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import axios from 'axios';
import { useUserStore } from "../../store/user.js";
let userStore = useUserStore();
userStore.storeInit();

const selectedTab = ref('notification');
const messages = ref([]);

const getMessages = () => {
  let recipient = ""
  if (userStore.identity === "教师"){
    recipient = "teacher";
  }else{
    recipient = "student"
  }
  axios.get(`http://localhost:9090/getMessages?recipient=${recipient}`)
    .then(response => {
      messages.value = response.data;
      // console.log(messages.value);
    })
    .catch(error => {
      console.error("获取消息失败", error);
    });
};

const selectTab = (tab) => {
  selectedTab.value = tab;
};

const viewFullMessage = (message) => {
  // 查看完整消息的逻辑
};

onMounted(() => {
  getMessages();
});

const navigateTo = (page) => {
  window.location.href = `/src/pages/${page}`;
};
</script>

<style scoped>
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  background-color: #fff;
  border-bottom: 1px solid #ddd;
}

.header .title {
  font-size: 24px;
  color: #1e90ff;
}

.header .user-info {
  display: flex;
  align-items: center;
}

.header .user-info .notification {
  margin-right: 20px;
  position: relative;
}

.header .user-info .notification .badge {
  position: absolute;
  top: -5px;
  right: -5px;
  background-color: red;
  color: #fff;
  border-radius: 50%;
  padding: 2px 5px;
  font-size: 12px;
}

.header .user-info .user {
  margin-right: 10px;
  color: #333;
}

.header .user-info .actions a {
  margin: 0 5px;
  color: #1e90ff;
  text-decoration: none;
}

.el-header {
  background-color: #fff;
  padding: 10px;
  border-bottom: 1px solid #ddd;
}

.breadcrumb-nav {
  padding: 0 20px;
}

.content-wrapper {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.message-nav {
  margin-bottom: 20px;
}

.message-card {
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 15px;
  margin-bottom: 20px;
  background-color: #fff;
}

.message-card h3 {
  font-size: 18px;
  margin: 0 0 10px;
  font-weight: bold;
}

.message-card p {
  margin: 0 0 10px;
}

.message-card .message-meta {
  color: #888;
  font-size: 14px;
}
</style>
